/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ACRestWebService;

import AjouConquerorBeans.Nowuser;
import data.customLocation;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.enterprise.context.RequestScoped;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;

/**
 * REST Web Service
 *
 * @author ajou
 */
@Path("location")
@RequestScoped
public class LocationResource {

    Nowuser nowuser = lookupNowuserBean();
    @Context
    private UriInfo context;

    /**
     * Creates a new instance of LocationResource
     */
    public LocationResource() {
    }

    /**
     * Retrieves representation of an instance of resources.LocationResource
     *
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("application/json")
    public String getJson() {
        return "AC";
    }

    @POST
    @Produces("application/json")
    @Consumes("application/json")
    public ArrayList<customLocation> doPost(customLocation location) {
        int roomid = nowuser.getRoomById(location.getId());
        if (roomid != -1) {
            ArrayList<customLocation> users = nowuser.getUsers(roomid);
            nowuser.setLocation(roomid, location);
            return users;
        }
        return null;
    }

    /**
     * PUT method for updating or creating an instance of LocationResource
     *
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("application/json")
    public void putJson(String content) {
    }

    private Nowuser lookupNowuserBean() {
        try {
            javax.naming.Context c = new InitialContext();
            return (Nowuser) c.lookup("java:global/AjouConquerorServer/AjouConquerorServer-ejb/Nowuser!AjouConquerorBeans.Nowuser");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
